package com.cust.zy.newtask.activity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;

import com.cust.zy.newtask.R;
import com.cust.zy.newtask.dao.DataOperation;
import com.cust.zy.newtask.model.Task;
import com.cust.zy.newtask.model.Word;
import com.cust.zy.newtask.tools.TaskResult;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created by zhuangyuan on 4/12/2017.
 * 邮箱：zhuangyuan.k@qq.com
 */
public class AlterTaskActivity extends AppCompatActivity {
    private final static String LOG = "AlterTaskActivity";
    private EditText taskName;
    private EditText taskDescription;
    private DataOperation dataOperation;
    private ProgressBar progressBar;

    Task selectedTask ;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alter_task);
        dataOperation = new DataOperation(AlterTaskActivity.this);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_new_plan);
        progressBar = (ProgressBar) toolbar.findViewById(R.id.toolbar_progress_bar);
        toolbar.setTitle("修改任务");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        Bundle bundle = this.getIntent().getExtras();
        selectedTask = bundle.getParcelable(TaskListActivity.SELECTED_TASK);

        taskName = (EditText) this.findViewById(R.id.alter_task_name);
        taskDescription = (EditText) this.findViewById(R.id.alter_task_description);
        taskName.setText(selectedTask.getName());
        taskDescription.setText(selectedTask.getDescription());

        Button subButton = (Button) this.findViewById(R.id.alter_task_subBtn);
        subButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectedTask.setName(taskName.getText().toString());
                selectedTask.setDescription(taskDescription.getText().toString());
                progressBar.setVisibility(View.VISIBLE);
                AlterTask alterTask = new AlterTask();
                alterTask.execute();
            }
        });
    }
    private class AlterTask extends AsyncTask<String, Object, TaskResult<Boolean>> {

        @Override
        protected TaskResult<Boolean> doInBackground(String... arg0) {
            try {
                try {
                    dataOperation.AlterTask(selectedTask);
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.e(LOG, e.toString());
                }
                return new TaskResult<Boolean>(true);
            } catch (Exception e) {
                return new TaskResult<Boolean>(e);
            }
        }

        @Override
        protected void onPostExecute(TaskResult<Boolean> result) {
            if(result.isValid()){
                Intent intent = new Intent(AlterTaskActivity.this, TaskListActivity.class);
                startActivity(intent);
                finish();
            }
        }
    }
}
